
package com.rinvay.asdownloader.core.access;

import com.rinvay.asdownloader.platform.AndroidFactory;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;


/**
 * Wifi access network
 * 
 */
public class WifiNetworkAccess extends NetworkAccess {
	/**
	 * Wi-Fi manager
	 */
	private WifiManager wifiManager;


	/**
	 * Constructor
	 * 
     * @throws CoreException
	 */
	public WifiNetworkAccess() {
		super();

		// Get wi-Fi info
		wifiManager = (WifiManager)AndroidFactory.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

//		if (logger.isActivated()) {
//    		logger.info("Wi-Fi access has been created (interface " + getType() + ")");
//    	}
    }
			
	/**
     * Connect to the network access
     * 
     * @param ipAddress IP address
     */
    public void connect(String ipAddress) {
//    	if (logger.isActivated()) {
//    		logger.info("Connecting to " + ipAddress);
//    	}
		this.ipAddress = ipAddress;
    }
    
	/**
     * Disconnect from the network access
     */
    public void disconnect() {
//    	if (logger.isActivated()) {
//    		logger.info("Disconnecting from " + ipAddress);
//    	}
    	ipAddress = null;
    }
    
	/**
	 * Return the type of access
	 * 
	 * @return Type
	 */
	public String getType() {
		WifiInfo info = wifiManager.getConnectionInfo();
		if (info.getLinkSpeed() <= 11) {
			return "IEEE-802.11b";
		} else {
			return "IEEE-802.11a";
		}
	}    
	
	/**
	 * Return the network name
	 * 
	 * @return Name
	 */
	public String getNetworkName() {
		String name = "Wi-Fi ";
		WifiInfo info = wifiManager.getConnectionInfo();
		if (info.getLinkSpeed() <= 11) {
			name += "802.11b";
		} else {
			name += "802.11a";
		}
		name += ", SSID=" + wifiManager.getConnectionInfo().getSSID();
		return name;
	}	
}
